<?php


namespace app\api\controller;


use app\common\controller\Api;
use app\common\model\order\OrderItem;
use app\common\model\UserCards;
use think\Model;

class Aftersales extends Api
{
    // 无需登录的接口,*表示全部
    protected $noNeedLogin = [];
    // 无需鉴权的接口,*表示全部
    protected $noNeedRight = ['*'];

    public function addaftersales()
    {
        $param =$this->request->post();
        $order=(new \app\common\model\order\Order())
            ->where(['id'=>$param['order_id'],'user_id'=>$this->auth->id,'status'=>1])->find();
        if(!$order){
            $this->error('订单不存在');
        }
        if($order['appoints']!=$order['canuse_appoints']){
            $this->error('不可以退款');
        }
        $count1=  (new UserCards())->where(['order_id'=>$param['order_id']])->count();
        if($count1>0){
           $count2 = (new UserCards())->where(['order_id'=>$param['order_id'],'status'=>1])
                ->where('rights_endtime','<=',time())->count();
           if($count2>0){
               $this->error('不可以退款');
           }
            $count=  (new UserCards())->where(['order_id'=>$param['order_id'],'status'=>1])->count();
            if($count1!=$count){
                $this->error('不可以退款');
            }

        }
        $order_item =(new OrderItem())->where(['order_id'=>$param['order_id']])->select();
        if(count($order_item)<1){
            $this->error('订单不存在');
        }
        $saves[]=[];
        foreach ($order_item as $k=>$v){
            $saves=[
                'aftersale_sn'=>getOrderNo($this->auth->id),
                'user_id'=>$this->auth->id,
                'reason'=>$param['reason'],'images'=>implode(',',changetype($param['images'])),
                'order_id'=>$param['order_id'],'content'=>$param['content'],'order_item_id'=>$v['id'],
                'refund_fee'=>$v['total_fee'],'goods_title'=>$v['goods_title'],
                'goods_image'=>$v['goods_image'],'goods_num'=>$v['goods_num'],
                'goods_original_price'=>$v['goods_original_price'],
                'aftersale_status'=>1,
                'pay_price'=>$v['total_fee'],
                'real_money'=>$v['total_fee'],
                'rights_endtime'=>$v['rights_endtime'],
                'goods_price'=>$v['goods_price']
            ];
            (new UserCards())->where(['order_item_id'=>$v['id']])->update(['is_show'=>2]);
        }




        $res=(new \app\common\model\order\Aftersales())->save($saves);
        if($res){
            (new \app\common\model\order\Order())
                ->where(['id'=>$param['order_id'],'user_id'=>$this->auth->id,'status'=>1])
                ->update(['refund_status'=>1]);
            (new OrderItem())
                ->where(['order_id'=>$param['order_id']])
                ->update(['refund_status'=>1]);
            $this->success('');
        }else{
            $this->error('申请退款失败');
        }

    }

    public function orderitemDetail()
    {
        $param =$this->request->param();
        $order=(new \app\common\model\order\Order())
            ->where(['id'=>$param['order_id'],'user_id'=>$this->auth->id,'status'=>1,'refund_status'=>0])->find();
        if(!$order){
            $this->error('订单不存在');
        }
        if($order['appoints']!=$order['canuse_appoints']){
            $this->error('不可以退款');
        }
        $data=\app\common\model\order\Order::with(['orderitem'])
            ->where(['user_id'=>$this->auth->id,'id'=>$param['order_id']])
            ->find();
        $this->success('',$data);
    }
}